package org.velocity;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * Velocity 测试demo
 * Created by Administrator on 2016/12/8.
 */
public class Test {

    public static void main(String[] args){
        // Velocity HelloWorld
        helloworld();
    }

    public static void helloworld(){
        // 初始化模板引擎
        VelocityEngine ve = new VelocityEngine();
        ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "file");
        ve.setProperty("file.resource.loader.description", "Velocity File Resource Loader");
        String resource_path = new Test().getClass().getResource("/").getPath()+"/resource";
        ve.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, resource_path);
        ve.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_CACHE, "true");
        ve.setProperty("file.resource.loader.modificationCheckInterval", "30");
        ve.init();
        // 获取模板文件
        Template t = ve.getTemplate("hellovelocity.vm");
        // 设置变量
        VelocityContext ctx = new VelocityContext();
        ctx.put("name", "Velocity");
        List list = new ArrayList();
        list.add("1");
        list.add("2");
        ctx.put("list", list);
        // 输出
        StringWriter sw = new StringWriter();
        t.merge(ctx,sw);
        System.out.println(sw.toString());
    }
}
